import PlatformUtil from "./Platforms/PlatformUtil";
import XmlHttpTool from "./MyTools/XmlHttpTool";
import Toasts from "./Export/BaseToasts";
const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Node)
    AwardDetail: cc.Node = null;

    @property(cc.Node)
    SuccessCell: cc.Node = null;

    @property(cc.Node)
    GiftDetailImage: cc.Node = null;

    @property(cc.Node)
    SuccessScroll: cc.Node = null;
    
    @property(cc.Node)
    WrechSuccessPopView: cc.Node = null;

    @property(cc.Label)
    GiftLabel: cc.Label = null;
    
    @property(cc.Label)
    CoinLabel: cc.Label = null;

    @property(cc.Node)
    StartGameNode : cc.Node = null;

    @property(cc.Node)
    CapsuleNode : cc.Node = null;
    
    @property(cc.Node)
    CapsuleBollNode : cc.Node = null;

    @property(Toasts)
    toasts: Toasts = null;

    @property
    isPlaying:boolean=false;

    @property([cc.Node])
    MealArray:Array<cc.Node>=[];

    @property(cc.SpriteFrame)
    MealNomarl:cc.SpriteFrame=null;

    @property(cc.SpriteFrame)
    MealSelect:cc.SpriteFrame=null;

    @property(cc.Node)
    CapsuleBoom : cc.Node = null;

    @property(cc.ScrollView)
    GiftScrollView : cc.ScrollView = null;
  
    @property(sp.SkeletonData)
    CapsuleSke : sp.SkeletonData = null;

    @property
    PrizeNumbers:number=1

    @property
    Play_type:number=1
    
    @property(cc.Label)
    RoomLabel : cc.Label = null;
    
    @property(cc.Node)
    RibbonNodeOne : cc.Node = null;
   
    @property(cc.Node)
    RibbonNodeTwo : cc.Node = null;

    @property([cc.SpriteFrame])
    Bollsprite:Array<cc.SpriteAtlas>=[];

    @property
    boolNumber:number=0;
    
    @property
    QuantitylNumber:number=0;

    @property
    roomCost:number=0;

    @property({type:cc.AudioClip})
    eggBoomSound = null;

    @property({type:cc.AudioClip})
    bgMusicSound = null;

    @property
    chooseNumber:number=1;

    @property(cc.Node)
    CapsuleMask : cc.Node = null;

    onLoad () {

        PlatformUtil.init();
        PlatformUtil.platform.init(this);
        
        this.roomGetInfo()
        var isSwitchSound=PlatformUtil.platform.enableSound();
        var isSwitchSoundBg=PlatformUtil.platform.enableBGM();
        (<any>window).isSound=isSwitchSound
        
        switch (cc.sys.os){
          case cc.sys.OS_ANDROID:
          if(isSwitchSoundBg){
             cc.audioEngine.play(this.bgMusicSound, true, 1);
          }
          break;
          case cc.sys.OS_IOS:
          PlatformUtil.platform.setBGM()
           break;

           default:
           break;

      } 

      
      this.chooseNumber=1
      this.getUserEggCoin()
    
    }

    start () {

    }

    roomGetInfo(){

        var that = this
        XmlHttpTool.instance.httpUtils("/capsule_eggs/getinfo", {}, function (successRes) {
    
          cc.log("扭蛋情列表", successRes);
          PlatformUtil.platform.loadSuccess()
          that.roomCost=successRes["cost"]
          that.QuantitylNumber=successRes["quantity"]
          that.setRoomBtnDetail()
          that.RoomLabel.string="库存:"+successRes["quantity"]+"\n"+successRes["cost"]+"币/次"
          var _this = that
          cc.loader.load({ url: successRes["thumbnail"], type: 'png' }, function (err, img) {
    
            var mylogo = new cc.SpriteFrame(img);
            _this.AwardDetail.getComponent(cc.Sprite).spriteFrame = mylogo
    
          });
     
        }, function (faileRes) {
          cc.log("扭蛋情列表失败", faileRes);
          PlatformUtil.platform.loadFail()
    
        }, function (errorRes) {
      
           cc.log("网络错误");
        });

    }

    setRoomBtnDetail(){

      for(let i=0; i<this.MealArray.length;i++){

        switch(i){
          case 0:
          this.MealArray[i].getChildByName("Desc").getComponent(cc.Label).string='免费试手气'
          break
          case 1:
          this.MealArray[i].getChildByName("Desc").getComponent(cc.Label).string=this.roomCost+'币/次'
          break
          case 2:
          this.MealArray[i].getChildByName("Desc").getComponent(cc.Label).string=this.roomCost*4+'币/次'
          break
          case 3:
          this.MealArray[i].getChildByName("Desc").getComponent(cc.Label).string=this.roomCost*8+'币/次'
          break
          default:
          break
        }
       
      }
    }

    startGameWrench(){


      // this.onPlay()
      // return
      // 

      if(this.isPlaying==true){
        // this.toasts.add("你操作太频繁");
         return
      }
  
      this.isPlaying=true
      this.gameOverPop()
    
    }


    bollAnimates(){

      this.CapsuleBollNode.active=false
      this.CapsuleMask.active=false
      var  btnAnimates =   this.StartGameNode.getComponent(cc.Animation);//获取动画列表
      btnAnimates.play(btnAnimates.getClips()[0].name)

      this.scheduleOnce(function(){

        var  CapsuleAnimates =   this.CapsuleNode.getComponent(cc.Animation);//获取动画列表
        CapsuleAnimates.play(CapsuleAnimates.getClips()[0].name)
        this.CapsuleNode.getComponent("ExSound")._playSound();

      }, 0.8);


       this.scheduleOnce(function(){

        var  CapsuleAnimates =   this.CapsuleNode.getComponent(cc.Animation)
        CapsuleAnimates.stop(CapsuleAnimates.getClips()[0].name)
        var boolNumber = Math.floor(Math.random()*3)

         if(boolNumber<this.Bollsprite.length){
             this.CapsuleBollNode.getChildByName("capsule_red").getComponent(cc.Sprite).spriteFrame=this.Bollsprite[boolNumber]
         }
        this.boolNumber=boolNumber
        this.CapsuleBollNode.active=true
        this.CapsuleMask.active=true
        var CapsuleBollAnimates =   this.CapsuleBollNode.getComponent(cc.Animation);//获取动画列表
        CapsuleBollAnimates.play(CapsuleBollAnimates.getClips()[0].name)
  
        // 
  
       this.scheduleOnce(function(){
           this.onPlay();
        }, 0.2);
        
      }, 3);

    }

    onPlay() {

        var moveAction= cc.moveTo(1,0,0);
        this.CapsuleBollNode.runAction(moveAction.easing(cc.easeExponentialOut())); 

        this.scheduleOnce(function(){

          var that=this

          this.CapsuleBollNode.setPosition(-266,-293); 
          // this.CapsuleMask.active=false
          this.CapsuleBollNode.active=false
          this.CapsuleBoom.active=true
          let spine = that.CapsuleBoom.getComponent(sp.Skeleton);
  
          cc.loader.loadRes("capsule/skeleton",sp.SkeletonData,function(err,skdata){
            cc.log("骨骼",skdata)
            spine.setSkeletonData=skdata
          
          })
          if(this.boolNumber==0){
            spine.setSkin("red")
          }else if(this.boolNumber==1){
            spine.setSkin("green")
          }else{
            spine.setSkin("yellow")
          }
          
          spine.setAnimation(1, "animation", false);

          this.scheduleOnce(function(){
            this.WrechSuccessPopView.active=true
            this.isPlaying=false
            if(isSound){
              cc.audioEngine.playEffect(that.eggBoomSound, false);
            }
          
         }, 0.3);
      
       }, 0.4);



    }

   gameOverPop(){

       var that = this
        XmlHttpTool.instance.httpUtils("/capsule_eggs/catch", {"play_type":this.Play_type,"play_num":this.PrizeNumbers}, function (successRes) {
    
          cc.log("成功开始扭蛋", successRes);
          that.bollAnimates()
    
          that.QuantitylNumber=successRes["quantity"]
          switch(that.chooseNumber){
            case 0:
            that.RoomLabel.string="库存:"+that.QuantitylNumber+"\n"+"免费/次"
            break
            case 1:
            that.RoomLabel.string="库存:"+that.QuantitylNumber+"\n"+that.roomCost+"币/次"
            break
            case 5:
            that.RoomLabel.string="库存:"+that.QuantitylNumber+"\n"+that.roomCost*4+"币/次"
            break
            case 10:
            that.RoomLabel.string="库存:"+that.QuantitylNumber+"\n"+that.roomCost*8+"币/次"
            break
            default:
            break
          }
          that.getUserEggCoin()
          that.SuccessScroll.removeAllChildren();
          var _this = that
          for(let toy of successRes["toy_info"]){

            cc.loader.load({ url: toy["thumbnail"], type: 'png' }, function (err, img) {
    
              var mylogo = new cc.SpriteFrame(img);
              _this.GiftDetailImage.getComponent(cc.Sprite).spriteFrame = mylogo
              _this.GiftDetailImage.getComponent(cc.Sprite).sizeMode = cc.Sprite.SizeMode.TRIMMED;
              _this.GiftLabel.string=toy["name"]
              var item = cc.instantiate(that.SuccessCell);
              that.SuccessScroll.addChild(item);
              
            });

           

          }
    
        that.RibbonNodeOne.getComponent(cc.ParticleSystem).resetSystem();
        that.RibbonNodeTwo.getComponent(cc.ParticleSystem).resetSystem();
        that.GiftScrollView.scrollToLeft(0.1);
      
        }, function (faileRes) {

          cc.log("扭蛋失败失败", faileRes);
          if(faileRes["errcode"]==4006){
            that.toasts.add("扭蛋币不足")
            that.CapsuleMask.active=false
          }else if(faileRes["errcode"]==8006){
            that.toasts.add("商品库存不足")
          }
    
          that.isPlaying=false
  
        }, function (errorRes) 
        {
          that.isPlaying=false
        });

   }


   MealChooseClick(event,customEventData){

     cc.log("点击选项",customEventData)

     if(customEventData==0){
      this.Play_type=2
      this.chooseNumber=0
      this.RoomLabel.string="库存:"+this.QuantitylNumber+"\n"+"免费/次"

      this.PrizeNumbers=1
     }else if(customEventData==1){
      this.Play_type=1
      this.PrizeNumbers=1
      this.chooseNumber=1
      this.RoomLabel.string="库存:"+this.QuantitylNumber+"\n"+this.roomCost+"币/次"
     }else if(customEventData==2){
      this.Play_type=1
      this.PrizeNumbers=5
      this.chooseNumber=5
      this.RoomLabel.string="库存:"+this.QuantitylNumber+"\n"+this.roomCost*4+"币/次"
     }else{
      this.Play_type=1
      this.PrizeNumbers=10
      this.chooseNumber=10
      this.RoomLabel.string="库存:"+this.QuantitylNumber+"\n"+this.roomCost*8+"币/次"
     }

     for(let i=0; i<this.MealArray.length;i++){
          
         if(i==customEventData){
           this.MealArray[i].getComponent(cc.Sprite).spriteFrame=this.MealSelect
           var colorCode =  new cc.Color(150,30,35,255);
           this.MealArray[i].getChildByName("Ttile").color=colorCode
           this.MealArray[i].getChildByName("Ttile").getComponent(cc.LabelOutline).color=colorCode
           this.MealArray[i].getChildByName("Desc").color=colorCode
           this.MealArray[i].getChildByName("Desc").getComponent(cc.LabelOutline).color=colorCode
        
         }else{
          this.MealArray[i].getComponent(cc.Sprite).spriteFrame=this.MealNomarl
          var colorCode =  new cc.Color(168,227,242,0);
          this.MealArray[i].getChildByName("Ttile").color=colorCode
          this.MealArray[i].getChildByName("Ttile").getComponent(cc.LabelOutline).color=colorCode
          this.MealArray[i].getChildByName("Desc").color=colorCode
          this.MealArray[i].getChildByName("Desc").getComponent(cc.LabelOutline).color=colorCode
         }
             
     }
   }


    getUserEggCoin(){

        var that = this
        XmlHttpTool.instance.httpUtils("/users/coin_eggs", {"play_type":1}, function (successRes) {
    
          (<any>window).gCoin=successRes["stars"]
          cc.log("用户的扭蛋币数量", gCoin);
          that.CoinLabel.string=gCoin
      
        }, function (faileRes) {

    
        }, function (errorRes) {
        });

    }


    refreCoinClick(){

      this.getUserEggCoin()
    }

    backApp(){

      PlatformUtil.platform.exit()
    }

    // update (dt) {}
}
